home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Best of www.BestZips.com (Collector's Edition)
/
Best of WWW.BESTZIPS.COM Collector's Edition (JCSM Shareware) (JCS Marketing).ISO
/
tutorial
/
adatu311.zip
/
PRINT.ME
< prev
next >
Wrap
Text File
|
1996-01-10
|
88KB
|
2,167 lines
Welcome to
A D A - T U T R
The Interactive Ada Tutor
by John J. Herro, Ph.D.
Software Innovations Technology
These are the printed course notes.
Ver. 3.11, 10 Jan. 1996
Copyright 1988-1996 John J. Herro
SOFTWARE INNOVATIONS TECHNOLOGY
1083 MANDARIN DR NE
PALM BAY FL 32905-4706
(407)951-0233
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor
johnherro@aol.com
or: john.herro%374-38-2@satlink.oau.org
You may make copies of these notes,
in printed or machine-readable form.
You may also copy the computer program.
Please see page 1 for details.
Page i
TABLE OF CONTENTS
Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . ii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Registration and Licenses - What is Shareware? . . . . . . . . . . 2
Special Offer to Companies, Schools, Etc. . . . . . . . . . . . . 3
Note to Compiler Companies . . . . . . . . . . . . . . . . . . . . 5
Does Your Screen Show Strange Characters Like Arrows and Brackets? 6
The Ada Reserved Words . . . . . . . . . . . . . . . . . . . . . . 7
Steps for Outside Assignment 1, Preparing to Run Ada . . . . . . . 8
Listings of HELLO.ADA and ADD.ADA . . . . . . . . . . . . . . . . 9
Listing of TRITEST.ADA . . . . . . . . . . . . . . . . . . . . . . 10
Steps for Outside Assignment 2, Exercise in Enumeration Types . . 12
Listing of NEXTDATE.ADA . . . . . . . . . . . . . . . . . . . . . 13
Steps for Outside Assignment 3, Exercise in Records . . . . . . . 14
Listing of FIBTEST.ADA . . . . . . . . . . . . . . . . . . . . . . 15
Steps for Outside Assignment 4, Exercise in Recursion . . . . . . 16
Simplified Specification for TEXT_IO . . . . . . . . . . . . . . . 17
Listings of Procedure FILECOPY and Function EXISTS . . . . . . . . 19
Requirements for the Program LEDIT . . . . . . . . . . . . . . . . 20
Steps for Outside Assignment 5, Writing a Simple Line Editor . . . 25
How to Test LEDIT . . . . . . . . . . . . . . . . . . . . . . . . 26
Listing of LEDIT.ANS . . . . . . . . . . . . . . . . . . . . . . . 29
Listing of TASKING.DUM . . . . . . . . . . . . . . . . . . . . . . 33
Steps for Outside Assignment 6, Exercise in Tasking . . . . . . . 34
Output from TASKING.EXE After Modification of TASKING.ADA . . . . 35
Listing of TASKING.ANS . . . . . . . . . . . . . . . . . . . . . . 36
Appendix A: Would You Like a Textbook to Use Along with ADA-TUTR? 37
Appendix B: Running BOOK to Print All the Screens . . . . . . . . 38
Appendix C: Some Ada Compilers Available for the PC . . . . . . . 39
Appendix D: Sample Multi-User Software License . . . . . . . . . 41
Appendix E: Installing ADA-TUTR on Other Computers . . . . . . . 43
Appendix F: We Listen! New Features of ADA-TUTR . . . . . . . . 48
Appendix G: Disclaimer of Warranty . . . . . . . . . . . . . . . 50
Page ii
ACKNOWLEDGMENTS
The following trademarks are used in these notes and in the
accompanying files:
ActivAda Thomson Software Products
DOS Microsoft Corp.
IBM International Business Machines Corp.
Janus/Ada R.R. Software, Inc.
Open Ada Meridian Software Systems, Inc.
UNIX AT & T
VAX Digital Equipment Corporation
VMS Digital Equipment Corporation
Windows Microsoft Corp.
Windows 95 Microsoft Corp.
Windows NT Microsoft Corp.
The file ONECHAR.C was sent to us by Mr. David Hill, 7549 Wynford
Street, Salt Lake City, UT 84121, and the file ALTCHAR.C was sent to
us by Mr. Richard Conn, 22 Hunt Court, Tinton Falls, NJ 07753. We're
very grateful to Mr. Hill and to Mr. Conn for giving us permission to
include these files in ADA-TUTR. When used with UNIX.ADA, these files
provide two ways to run the Tutor on UNIX based machines without
having to strike ENTER after each response. See pages 43-47.
The programs in this version of ADA-TUTR were compiled with Janus/Ada
version 2.2.2b, and we're grateful to R.R. Software, Inc. for
permission to distribute the .COM files without restriction.
Page 1
INTRODUCTION
ADA-TUTR, the Interactive Ada Tutor, will make you an excellent
Ada programmer in minimum time. You'll learn good Ada program design
techniques, not just Ada syntax. ADA-TUTR runs on PCs as well as
workstations and mainframes. On PCs an Ada compiler is helpful, but
not required. The PC can have any monochrome or color monitor.
These printed notes aren't meant to be a complete course, or even
a summary, of Ada. They merely accompany the program ADA-TUTR, which
is a complete course. You can't "study" these printed notes alone.
For now, just read pages 1, 2, and 6.
ADA-TUTR lets you learn at your own pace. So that you don't feel
pressured, ADA-TUTR doesn't keep track of the number of right and
wrong answers. It simply tells you whether your answers are correct,
and why. Also, there's no time limit for answering the questions or
completing the Outside Assignments.
Because I want every programmer to have a chance to learn the
best programming language, I made ADA-TUTR available very economically
as shareware. Shareware isn't free, but you may TRY this program for
free, and register or buy a license only if you use it. Please see
pages 2-3 for details.
Whether or not you use ADA-TUTR and register (or buy a license),
please make unmodified copies of the program and distribute them on
bulletin boards, at clubs, and give them to companies, schools, etc.
You may charge a small fee to copy the program, provided you make it
clear that the fee is only for the copy, and doesn't pay for the
shareware. (Companies that copy shareware usually charge about $2 to
$10 per diskette.) Bulletin boards may carry ADA-TUTR even if they
charge for access, and you may include ADA-TUTR on CD-ROMs. Please
contact us for permission to include ADA-TUTR with other commercial
products, such as Ada compilers (see page 5). You may add your own
files if you want. (The files included with ADA-TUTR are briefly
described in FILES.LST.) If you register, you can earn substantial
money distributing ADA-TUTR; please see page 2 for details.
Ada will become more and more important, since the Department of
Defense mandated its use in defense software, and is now making it
very hard to obtain waivers. Although learning Ada takes real effort,
you'll be able to write software that's more reliable and easier to
modify a month or a year later.
New features of this version of ADA-TUTR are described on pages
48-49. Please send me your comments and suggestions. I wish you
success using ADA-TUTR!
John J. Herro, Ph.D. SOFTWARE INNOVATIONS TECHNOLOGY
1083 MANDARIN DR NE
johnherro@aol.com PALM BAY FL 32905-4706
john.herro%374-38-2@satlink.oau.org (407) 951-0233
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor
Page 2
REGISTRATION AND LICENSES - WHAT IS SHAREWARE?
Shareware is a way of marketing a program. It lets you try the
program before spending money for it. If you decide to use the
program, you send a small payment to register your copy or buy a
license. We give you several incentives to do so, explained below.
You're on your honor; you know whether you're "using" ADA-TUTR or
only "trying" it.
To use ADA-TUTR, individuals register as described below.
Companies, schools, and other organizations can register each
individual who uses the program, or buy a Multi-User License as
described on the next page.
INDIVIDUALS:
Register your copy for only $25. When you register, please give
us the serial number from the ADA-TUTR opening screen. We'll assign
you a new serial number. Then run CHANGESN to put your new number
into the program, and give copies to individuals, schools, companies,
clubs, computer bulletin boards, etc.
We'll send you a commission of $5 for each individual who
registers from one of your copies, and TEN PERCENT for each Multi-User
License sold from one of your copies! As you can see from the next
page, you can earn up to $240 per license sold! Commissions are paid
quarterly. If you like, print the file AD.TXT and use copies as an
advertisement.
Registration includes the right to use ADA-TUTR as long as you
like, and it includes technical support for one year. While you have
technical support, we'll inform you of any important updates to
ADA-TUTR, and send you updated copies for only $10 each (free if you
send us a formatted diskette with a stamped diskette mailer). We're
planning another update of ADA-TUTR some time after Ada 95 compilers
become generally available. You can renew technical support for only
$10 a year. Even if you don't renew, you'll continue to receive
commissions.
When you register, you may order a copy of the current version of
ADA-TUTR, with your serial number. The price is $10, or free with
your formatted diskette and a stamped diskette mailer. If you would
like an order form or a price list, please print the file INVOICE.TXT.
We offer a Customization Kit that lets you edit, add, and delete
ADA-TUTR screens, questions, etc. (even whole topics), producing a new
ADA_TUTR.DAT file. See the last paragraph on page 4. Note that the
Customization Kit is not shareware.
We sell anywhere in the Free World. We're sorry, but we don't
yet accept credit cards. Outside the U.S., please remit in U.S. funds
and contact us about extra postal charges on diskettes.
COMPANIES, SCHOOLS, AND OTHER ORGANIZATIONS:
Please see our special offer for Multi-User Licenses on the next
page.
Page 3
SPECIAL OFFER TO COMPANIES, SCHOOLS, ETC.
ADA-TUTR can be of great benefit to your company, school, or
other organization. We encourage you to try ADA-TUTR before paying
for it, and you may install ADA-TUTR on your workstations and
mainframes as well as your PCs! Instructions for installing ADA-TUTR
on other computers are on page 43.
If you decide to use ADA-TUTR, you could register each individual
user, as described on page 2. But if you have more than 18 users, you
can save money by buying a Multi-User License, as described here.
You're on your honor; you know whether you're "using" ADA-TUTR or only
"trying" it. Multi-User Licenses are very reasonably priced, as
follows:
Number of Users: Price of License: Note:
Up to 100 only $450 Send $5 extra for a copy of the
Up to 500 only $725 latest version of ADA-TUTR on a
Up to 2000 only $950 5.25" diskette, $10 extra for a
Unlimited only $2400 copy on a 3.5" diskette.
A license may always be upgraded for only the difference in
price.
A sample Multi-User license appears on pages 41-42. A Multi-User
License gives the specified number of people the right to use ADA-TUTR
as long as you like, on all your computers (from small PCs to large
mainframes), at all your locations. We use the term Multi-User
License rather than Site License because there are no geographic
restrictions. (Note: If 100 people use ADA-TUTR now and 100 others
use it later, that counts as 200 users, not 100.) Sales commissions
are NOT paid to Multi-User Licensees.
A Multi-User License includes one year of technical support,
which can be renewed for only 10% of the price of the license per
year. While you have technical support, we'll inform you of any
important updates to ADA-TUTR, and send you an updated copy for only
$10. The update is free if you send a formatted diskette and a
stamped diskette mailer. We're planning another update of ADA-TUTR
some time after Ada 95 compilers become generally available.
Even if you don't renew your Technical Support, you'll still have
the right to use ADA-TUTR as long as you like.
Please print the file INVOICE.TXT, or use your own form. (When
ordering, please give us the serial number from the opening screen of
your copy of ADA-TUTR, if you have one.)
We accept Purchase Orders. Our Federal tax ID number is
360-40-8382, and we qualify as a Small Business. We sell anywhere in
the Free World. We're sorry, but we don't yet accept credit cards.
Outside the U.S., please remit in U.S. funds and contact us about
extra postal charges on diskettes.
Page 4
With shareware, you never have to worry about software piracy,
because your people are ENCOURAGED to copy and distribute the program!
If you prefer, however, a non-shareware version of ADA-TUTR is
available at the same prices.
If you buy a license, we'll be happy to do minor customization of
ADA-TUTR for you at no charge. For example, we can add your company
name to the opening screen. If you'd like major customization,
contact us for terms, or buy our Customization Kit for $195. Using
any text editor on a PC, workstation, or mainframe, the Customization
Kit lets you edit, add, and delete screens, questions, etc. (even
whole topics), producing a new ADA_TUTR.DAT file. The Customization
Kit isn't shareware, but you may copy it for use throughout your
organization as required.
Page 5
NOTE TO COMPILER COMPANIES
A special version of ADA-TUTR is available, without the shareware
notices and without the list of Ada compilers. It can be customized
for your company, and can run on almost any Ada platform, including
workstations and mainframes, because Ada source code is included.
ADA-TUTR can greatly increase the sales appeal of your compilers and
your Ada tools, whether you sell it separately or include it with your
products. Please contact us.
If you're a prospective contractor or subcontractor, the
shareware or special version of ADA-TUTR may be the best and most
economical way for you to supply your customer's Ada educational
needs. Please contact us before writing your proposal.
Page 6
DOES YOUR SCREEN SHOW STRANGE CHARACTERS LIKE ARROWS AND BRACKETS?
Give the command TYPE READ.ME on your PC. If one word appears
brighter than the rest, you can skip this page. However, if your
screen shows strange characters like "[1m" when you TYPE READ.ME or
run ADA_TUTR, you need to read this.
ADA-TUTR uses ANSI (American National Standards Institute) escape
sequences for highlighting, cursor positioning, reverse video, etc.
Before ADA-TUTR will work correctly on a PC, you must install the
device driver ANSI.SYS, which came with your copy of DOS. To install
ANSI.SYS, do the following:
1. If there's a file CONFIG.SYS in the root directory of the disk
from which you boot, type it and look for a line saying
"DEVICE=ANSI.SYS" (without the quotes), in either upper or lower
case. If that line is not present, add it to CONFIG.SYS anywhere
in the file, using an ordinary text editor or word processor in
the non-document mode. If there's no CONFIG.SYS file, create one
containing the single line "DEVICE=ANSI.SYS" (without the quotes).
2. If there's no file ANSI.SYS in your root directory, copy ANSI.SYS
from your DOS distribution diskette or DOS subdirectory to the
root directory of the disk from which you boot.
3. Reboot the computer. ADA-TUTR should then work correctly.
Page 7
THE ADA RESERVED WORDS
The reserved words are listed in the Language Reference Manual in
section 2.9, and are repeated here for convenience.
abort declare generic of select
abs delay goto or separate
abstract * delta others subtype
accept digits out
access do if
aliased * in tagged *
all is package task
and pragma terminate
array private then
at procedure type
else limited protected *
elsif loop
end until *
entry use
begin exception raise
body exit mod range
record when
rem while
renames with
new requeue *
case for not return
constant function null reverse xor
* Ada 95 only.
Page 8
STEPS FOR OUTSIDE ASSIGNMENT 1, PREPARING TO RUN ADA
For the first Outside Assignment, learn enough about your Ada
compiler to compile and run the two simple programs HELLO.ADA and
ADD.ADA. Obviously, you have to do this before you can do the
remaining Outside Assignments.
1. Compile HELLO.ADA. On most systems, after the Ada compiler is
installed and your library is created, this only involves typing
ADA HELLO or ADA HELLO.ADA. On some systems, the compiler is invoked
from a menu or "APSE" (Ada Programming Support Environment).
2. Link, giving the name of the main program, HELLO. This usually
involves typing LINK HELLO or BIND HELLO, perhaps with some options.
On one system, the command is BAMP HELLO, for Build Ada Main Program!
3. Run the program. If your Ada compiler produces native code on a
PC, it created a file HELLO.EXE (or HELLO.COM), and this step simply
involves typing HELLO. On other systems, the command RUN HELLO is
appropriate. On some systems, you have to invoke an interpreter to
run the program.
4. Now compile, link, and run ADD.ADA.
If your compiler implements a subset of Ada, you may have to
modify the programs in this and later Outside Assignments to get them
to compile. Some subset Ada compilers don't allow "separate"
compilation (used in later assignments), and you'll have to include
the "separate" subprograms inside the main program and compile them as
one. Some subset compilers don't allow generic instantiation, and
have another way of displaying integers. In that case, you'll have to
modify ADD.ADA. If you're using a validated compiler, you won't have
to worry about any of this, because all of the Outside Assignments are
written in standard Ada.
Page 9
HELLO.ADA
---------
with Text_IO; use Text_IO;
procedure Hello is
begin
Put_Line("Hello!");
end Hello;
ADD.ADA
-------
with Text_IO; use Text_IO;
procedure Add is
package My_Int_IO is new Integer_IO(Integer); use My_Int_IO;
begin
Put(2 + 2);
New_Line;
end Add;
Page 10
TRITEST.ADA
-----------
with Text_IO; use Text_IO;
procedure Tritest is
Passed : Boolean := True;
type Triangle is (Equilateral, Isosceles, Scalene, Not_a_Triangle);
function Tritype(Len1, Len2, Len3 : in Integer) return Triangle
is separate;
procedure Compare(A, B, C: in Integer; Right_Answer : in Triangle)
is separate;
begin
Compare( 3, 4, 5, Scalene);
Compare( 6, 3, 4, Scalene);
Compare( 4, 3, 6, Scalene);
Compare( 3, 3, 3, Equilateral);
Compare( 3, 3, 4, Isosceles);
Compare( 3, 4, 3, Isosceles);
Compare( 4, 3, 3, Isosceles);
Compare( 7, 7, 4, Isosceles);
Compare( 7, 4, 7, Isosceles);
Compare( 4, 7, 7, Isosceles);
Compare( 1, 1, 1, Equilateral);
Compare( 0, 4, 4, Not_a_Triangle);
Compare( 4, 0, 4, Not_a_Triangle);
Compare( 4, 4, 0, Not_a_Triangle);
Compare( 0, 4, 3, Not_a_Triangle);
Compare( 3, 0, 4, Not_a_Triangle);
Compare( 4, 3, 0, Not_a_Triangle);
Compare(-1, 4, 4, Not_a_Triangle);
Compare( 4, -1, 4, Not_a_Triangle);
Compare( 4, 4, -1, Not_a_Triangle);
Compare(-1, 4, 3, Not_a_Triangle);
Compare( 3, -1, 4, Not_a_Triangle);
Compare( 4, 3, -1, Not_a_Triangle);
Compare( 2, 4, 6, Not_a_Triangle);
Compare( 1, 3, 2, Not_a_Triangle);
Compare( 3, 1, 2, Not_a_Triangle);
Compare( 1, 2, 4, Not_a_Triangle);
Compare( 1, 4, 2, Not_a_Triangle);
Compare( 4, 1, 2, Not_a_Triangle);
Compare( 0, 0, 0, Not_a_Triangle);
Compare( 0, 0, 4, Not_a_Triangle);
Compare( 0, 4, 0, Not_a_Triangle);
Compare( 4, 0, 0, Not_a_Triangle);
Compare( 3, 3, 7, Not_a_Triangle);
Compare( 3, 7, 3, Not_a_Triangle);
Compare( 6, 3, 3, Not_a_Triangle);
Compare(-3, -4, -5, Not_a_Triangle);
if Passed then
Put_Line("Congratulations, you completed the assignment!");
end if;
end Tritest;
-- continued --
Page 11
separate (Tritest)
procedure Compare(A, B, C: in Integer; Right_Answer : in Triangle) is
package Int_IO is new Integer_IO(Integer); use Int_IO;
package Tri_IO is new Enumeration_IO(Triangle); use Tri_IO;
My_Answer : Triangle := Tritype(A, B, C);
begin
if My_Answer /= Right_Answer then
Put("Sides:");
Put(A, Width => 3);
Put(B, Width => 3);
Put(C, Width => 3);
Put(" My answer: ");
Put(My_Answer, Width => 14);
Put(" Right answer: ");
Put(Right_Answer);
New_Line;
Passed := False;
end if;
end Compare;
Page 12
STEPS FOR OUTSIDE ASSIGNMENT 2, EXERCISE IN ENUMERATION TYPES
1. Compile the test driver TRITEST.ADA. Also, make a copy of the
dummy solution by typing COPY TRITYPE.DUM TRITYPE.ADA. You need
do this step only once.
2. Edit TRITYPE.ADA to become your real solution. You can skip this
step the first time through, to see error messages from the test
driver.
3. Compile your solution TRITYPE.ADA. If the compiler finds errors,
go back to step 2.
4. Link with the name of the main program TRITEST. Then execute. If
the test driver displays error messages, go back to step 2.
5. When the message "Congratulations, you completed the assignment!"
is displayed, you'll have a chance to compare your solution with
ours.
Page 13
NEXTDATE.ADA
------------
with Text_IO; use Text_IO;
procedure Nextdate is
type Month_Type is
(Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
subtype Day_Subtype is Integer range 1 .. 31;
type Date is
record
Day : Day_Subtype;
Month : Month_Type;
Year : Positive;
end record;
Passed : Boolean := True;
function Tomorrow(Today : in Date) return Date is separate;
procedure Display (S : in String; D : in Date) is
package Int_IO is new Integer_IO(Integer); use Int_IO;
begin
Put(S);
Put(D.Day, Width => 3);
Put(" " & Month_Type'Image(D.Month));
Put(D.Year, Width => 5);
New_Line;
end Display;
procedure Compare(Today, Right_Answer : in Date) is
My_Answer : Date := Tomorrow(Today);
begin
if My_Answer /= Right_Answer then
Display("Today: ", Today);
Display("My answer: ", My_Answer);
Display("Right answer:", Right_Answer);
New_Line;
Passed := False;
end if;
end Compare;
begin
Compare((12,Dec,1815), (13,Dec,1815)); -- ordinary date
Compare(( 3,Feb,1986), ( 4,Feb,1986)); -- ordinary date in Feb.
Compare((30,Jun,1981), ( 1,Jul,1981)); -- last day of 30-day month
Compare((30,Sep,3999), ( 1,Oct,3999)); -- last day of 30-day month
Compare((31,Mar,1876), ( 1,Apr,1876)); -- last day of 31-day month
Compare((31,Aug,1984), ( 1,Sep,1984)); -- last day of 31-day month
Compare((31,Dec,1966), ( 1,Jan,1967)); -- last day of year
Compare((28,Feb,1980), (29,Feb,1980)); -- leap year
Compare((28,Feb,1600), (29,Feb,1600)); -- century leap year
Compare((28,Feb,2200), ( 1,Mar,2200)); -- century non-leap year
Compare((28,Feb,1982), ( 1,Mar,1982)); -- non-leap year
Compare((29,Feb,1980), ( 1,Mar,1980)); -- leap day in leap year
if Passed then
Put_Line("Congratulations, you completed the assignment!");
end if;
end Nextdate;
Page 14
STEPS FOR OUTSIDE ASSIGNMENT 3, EXERCISE IN RECORDS
1. Compile the test driver NEXTDATE.ADA. Also, make a copy of the
dummy solution by typing COPY TOMORROW.DUM TOMORROW.ADA. You need
do this step only once.
2. Edit TOMORROW.ADA to become your real solution. You can skip this
step the first time through, to see error messages from the test
driver.
3. Compile TOMORROW.ADA. If the compiler finds errors, go back to
step 2.
4. Link with the name of the main program NEXTDATE. Then execute.
If the test driver displays error messages, go back to step 2.
5. When the message "Congratulations, you completed the assignment!"
is displayed, you'll have a chance to compare your solution with
ours.
Page 15
FIBTEST.ADA
-----------
with Text_IO; use Text_IO;
procedure Fibtest is
Passed : Boolean := True;
function Fib(N : in Positive) return Positive is separate;
procedure Compare (N : in Positive; Right_Answer : in Positive) is
package Int_IO is new Integer_IO(Integer); use Int_IO;
My_Answer : Positive := Fib(N);
begin
if My_Answer /= Right_Answer then
Put("N:"); Put(N);
Put(" My answer:"); Put(My_Answer);
Put(" Right answer:"); Put(Right_Answer);
New_Line;
Passed := False;
end if;
end Compare;
begin
Compare(1, 1);
Compare(2, 1);
Compare(3, 2);
Compare(4, 3);
Compare(5, 5);
Compare(6, 8);
Compare(7, 13);
Compare(10, 55);
Compare(15, 610);
Compare(20, 6765);
if Passed then
Put_Line("Congratulations, you completed the assignment!");
end if;
end Fibtest;
Page 16
STEPS FOR OUTSIDE ASSIGNMENT 4, EXERCISE IN RECURSION
1. Compile the test driver FIBTEST.ADA. Also, make a copy of the
dummy solution by typing COPY FIB.DUM FIB.ADA. You need do this
step only once.
2. Edit FIB.ADA to become your real solution. You can skip this step
the first time through, to see error messages from the test
driver.
3. Compile FIB.ADA. If the compiler finds errors, go back to step 2.
4. Link with the name of the main program FIBTEST. Then execute. If
the test driver displays error messages, go back to step 2.
5. When the message "Congratulations, you completed the assignment!"
is displayed, you'll have a chance to compare your solution with
ours.
Page 17
SIMPLIFIED SPECIFICATION FOR TEXT_IO
------------------------------------
package Text_IO is
type File_Type is limited private;
type File_Mode is (In_File, Out_File); -- Ada 9X adds Append_File.
type Count is ... (a user-defined type similar to Integer);
Status_Error, Mode_Error, Name_Error, End_Error : exception;
procedure Create (File : in out File_Type;
Mode : in File_Mode := Out_File;
Name : in String);
procedure Open (File : in out File_Type;
Mode : in File_Mode;
Name : in String);
procedure Close (File : in out File_Type);
procedure Delete (File : in out File_Type);
procedure New_Line (Spacing : in Count := 1);
procedure New_Line (File : in File_Type;
Spacing : in Count := 1);
procedure Skip_Line (Spacing : in Count := 1);
procedure Skip_Line (File : in File_Type;
Spacing : in Count := 1);
function End_Of_File (File : in File_Type) return Boolean;
procedure Put (Item : in Character);
procedure Put (File : in File_Type; Item : in Character);
procedure Get (Item : out Character);
procedure Get (File : in File_Type; Item : out Character);
procedure Put (Item : in String);
procedure Put (File : in File_Type; Item : in String);
procedure Put_Line (Item : in String);
procedure Put_Line (File : in File_Type; Item : in String);
procedure Get_Line (Item : out String; Last : out Natural);
procedure Get_Line (File : in File_Type;
Item : out String;
Last : out Natural);
generic
type Num is range <>;
package Integer_IO is
procedure Get (Item : out Num);
procedure Get (File : in File_Type; Item : out Num);
procedure Put (Item : in Num;
Width : in Integer := ...;
Base : in Integer := 10);
procedure Put (File : in File_Type;
Item : in Num;
Width : in Integer := ...;
Base : in Integer := 10);
end Integer_IO;
-- continued --
Page 18
generic
type Num is digits <>;
package Float_IO is
procedure Get (Item : out Num);
procedure Get (File : in File_Type; Item : out Num);
procedure Put (Item : in Num;
Fore : in Integer := 2;
Aft : in Integer := ...;
Exp : in Integer := 3);
procedure Put (File : in File_Type;
Item : in Num;
Fore : in Integer := 2;
Aft : in Integer := ...;
Exp : in Integer := 3);
end Float_IO;
generic
type Enum is (<>);
package Enumeration_IO is
procedure Get (Item : out Enum);
procedure Get (File : in File_Type; Item : out Enum);
procedure Put (Item : in Enum; Width : in Integer := 0);
procedure PUT (File : in File_Type;
Item : in Enum; Width : in Integer := 0);
end Enumeration_IO;
private
type File_Type is ... (implementation dependent);
end Text_IO;
Page 19
Program to copy a simple text file:
-----------------------------------
with Text_IO; use Text_IO;
procedure Filecopy Is
F1, F2 : File_Type;
S : String(1 .. 80);
Len : Integer;
begin
Put("Input file: "); Get_Line(S, Len);
Open(File => F1, Mode => In_File, Name => S(1 .. Len));
Put("Output file: "); Get_Line(S, Len);
Create(File => F2, Mode => Out_File, Name => S(1 .. Len));
while not End_Of_File(F1) loop
Get_Line(F1, S, Len);
Put_Line(F2, S(1 .. Len));
end loop;
Close(F1);
Close(F2);
end Filecopy;
Function to test if a text file exists:
---------------------------------------
with Text_IO; use Text_IO;
function Exists(File_Name : in String) return Boolean Is
F : File_Type;
Answer : Boolean := True;
begin
begin
Open(F, In_File, File_Name);
Close(F);
exception
when Name_Error => Answer := False;
end;
return Answer;
end Exists;
Page 20
REQUIREMENTS FOR THE PROGRAM LEDIT
This assignment will give you practice in writing a program of
greater complexity than the previous programs you've written. Imagine
that your screen editor is unavailable to a particular user, perhaps
because the user is dialing your computer from a remote location, and
your screen editor writes directly to the screen. You want to write a
very simple line editor, LEDIT, that could be used in such
circumstances. While your computer already has a line editor called
EDLIN, it's difficult to learn to use. LEDIT will take almost no
effort to learn. The only commands are LIST and EXIT! LEDIT edits by
means of line numbers, similar to early versions of Basic.
The user begins each typed line of text with a line number from 1
to 29999. Line numbers must be integers. We chose the upper limit
29999 so that the simple type Integer could be used - in any
implementation of Ada. Regardless of the order in which lines are
typed, LEDIT maintains a linked list of lines in order by number.
Also, line numbers may be preceded by any number of spaces. For
example, if the user types
40 -- This is a comment.
20 begin
10 with Text_IO; use Text_IO;
30 end Add;
and then types LIST, the editor will type
10 with Text_IO; use Text_IO;
20 begin
30 end Add;
40 -- This is a comment.
To INSERT lines, the user merely types lines with intermediate
line numbers. For example, if the user types
15 procedure Hello is
and then types LIST, LEDIT will type
10 with Text_IO; use Text_IO;
15 procedure Hello is
20 begin
30 end Add;
40 -- This is a comment.
To REPLACE an existing line, the user merely types a line with
the same line number as the line to be replaced. For example, if
15 procedure Add is
LIST
-- continued --
Page 21
is typed, LEDIT would then show
10 with Text_IO; use Text_IO;
15 procedure Add is
20 begin
30 end Add;
40 -- This is a comment.
Finally, to DELETE a line, the user merely types the number of
the line to be deleted, followed immediately by a carriage return.
Typing
40
LIST
would then produce
10 with Text_IO; use Text_IO;
15 procedure Add is
20 begin
30 end Add;
Thus the user can INSERT, REPLACE, and DELETE lines, all by line
numbers, without learning any commands. Note that in this simple
editor there is no "cursor" and no "current line."
The space is not required after the line number. These two lines
have exactly the same effect:
20 begin
20begin
Of course, if the text of the line begins with a digit, a space will
be required to separate it from the line number. In any event, LEDIT
always leaves one blank space after the line number when LISTing, for
readability. It always allows exactly five spaces for the line number
itself.
However, any EXTRA spaces typed after the line number are
significant. The three lines below each contain three EXTRA spaces
after the line number, for a total of four spaces.
24 Put(2 + 2);
26 New_LiNe;
18 package My_Int_IO is new Integer_IO(Integer); use My_Int_IO;
They have the effect of indenting the text three spaces. LIST now
shows
-- continued --
Page 22
10 with Text_IO; use Text_IO;
15 procedure Add is
18 package My_Int_IO is new Integer_IO(Integer); use My_Int_IO;
20 begin
24 Put(2 + 2);
26 New_Line;
30 end Add;
Although typing a line number followed immediately by a carriage
return deletes a line (if there is a line by that number), typing a
line number followed by a single space causes an empty line to be
introduced into the file. For example, typing 12 followed by a single
space and a carriage return would then cause LIST to type
10 with Text_IO; use Text_IO;
12
15 procedure Add is
18 package My_Int_IO is new Integer_IO(Integer); use My_Int_IO;
20 begin
24 Put(2 + 2);
26 New_Line;
30 end Add;
When LEDIT is run, it prompts for the names of the input and
output files. If the input file exists, LEDIT displays "File found"
and reads the file into its linked list, assigning line numbers
starting with 10 and incrementing by 10. If the file does not exist,
LEDIT displays "File not found," and the linked list is initially
empty. In any event, LEDIT creates an output file. When the EXIT
command is given, LEDIT writes the contents of the linked list to the
output file. In doing so, LEDIT removes the line numbers and the
first blank after each line number.
In the example above, let's assume that the user typed ADD.ADA
for an output file name. When the user types EXIT, the new file
ADD.ADA created by LEDIT will contain:
with Text_IO; use Text_IO;
procedure Add is
package My_Int_IO is new Integer_IO(Integer); use My_Int_IO;
begin
Put(2 + 2);
New_Line;
end Add;
(Note that the file contains one empty line.) Your program is not
allowed to add any trailing blanks of its own in the output file. If
the user again runs LEDIT and specifies ADD.ADA as an input file, the
editor will type "File found" and read the file. LIST will then show
-- continued --
Page 23
10 with Text_IO; use Text_IO;
20
30 procedure Add is
40 package My_Int_IO is new Integer_IO(Integer); use My_Int_IO;
50 begin
60 Put(2 + 2);
70 New_Line;
80 end Add;
When the user EXITs, the new file will contain modified text. The old
file will still be present until it is deleted.
The two commands LIST and EXIT must be accepted in either upper
or lower case, but for simplicity, they need not be accepted in a
mixture of cases. They may be preceded by any number of spaces. The
LIST command must be accepted in any of the following forms:
LIST (Lists all lines, if any, otherwise does nothing.)
list 30 (Lists line 30, if it exists.)
list 25 - 35 (Lists all lines between 25 and 35 inclusive, if any.)
LIST - 35 (Lists all lines numbered 35 or less, if any.)
list 25 - (Lists all lines numbered 25 or more, if any.)
Furthermore, all spaces are optional in the LIST command provided that
LIST is written solid, so that LIST25-35 is equivalent to the third
example above. Any other forms of LIST should be flagged as an error.
In particular, all of these should give error messages:
LIST -
list 2A
LIST 30 - 50 -
list 30 - -50
list 30-A
LIST XYZ
The EXIT command must stand alone; unlike LIST, EXIT is never
followed by anything. Except for the LIST and EXIT commands, every
line typed must begin with a number between 1 and 29999. (Of course,
the user should be able to type just a carriage return with no
effect.) LEDIT should check that line numbers are in range when adding
or replacing lines. The LIST command need not check the range of the
line numbers, because it should be impossible to create lines with
improper numbers. Your program need not handle input files so long
that 29999 would be exceeded when it assigns line numbers starting at
10 with an increment of 10.
Your LEDIT must prompt for input and output file names at the
start, and it may display a prompt for each line that the user types.
You may assume some maximum length for an input line (e.g., 80), and
assume that no file will have lines longer than the maximum. You may
also assume that no file will have special characters like form feeds.
-- continued --
Page 24
For simplicity, there's no way to edit a line except by retyping
it. Also, there's no way to copy or move a line or a block of lines,
and no way to delete a block of lines except one line at a time.
There's no RENUMBER command. The user can renumber the entire file
starting at 10 with an increment of 10 by EXITing and then rerunning
LEDIT. There's no AUTO command to make the editor type the line
numbers automatically while text is being typed by the user, and
there's no means of recovering from a system crash that occurs during
an edit. Additionally, there's no way to search for a string, or
replace one string with another. These features would all be
desirable, but we feel that Outside Assignment 5 is challenging as it
stands.
You're encouraged to use Ada's "separate" compilation feature, so
that you don't have to recompile the entire program when developing a
module.
As a point of reference, our solution to this assignment consists
of about 180 lines of code on four pages. If you have any questions
about what LEDIT should do, you can compile and run our solution,
which is in LEDIT.ANS.
The following declarations are offered by way of suggestion only,
and you should use them only if you feel comfortable with them:
Max_Length : constant Integer := 80;
type Text is
record
Len : Integer range 0 .. Max_Length;
Val : String(1 .. Max_Length);
end record;
type Link;
type P is access Link;
type Link is
record
Num : Positive;
Line : Text;
Next : P;
end record;
function Str(T : in Text) return String;
In our solution, we used type Text as above. However, we didn't
write a Text_Handler package, because it didn't seem to be needed. We
used a one-page main program with several "separate" subprograms.
Page 25
STEPS FOR OUTSIDE ASSIGNMENT 5, WRITING A SIMPLE LINE EDITOR
1. Carefully read the requirements starting on page 20 of these
notes. Take your time.
2. Write the Ada code, compile, and link. Call the main program
LEDIT. If you have any questions about what LEDIT should do, you
can compile and run our solution, which is in LEDIT.ANS.
3. Refer to pages 26-28 of these notes for instructions on testing
your line editor. If any tests are failed, go back to step 2.
4. When all the tests are passed, you've completed the assignment and
will have a chance to compare your solution with ours.
Page 26
HOW TO TEST LEDIT
1. Run LEDIT and give the name of an input file that doesn't exist.
LEDIT should say "File not found." Give another name of a file that
doesn't exist for an output file.
2. Type "LIST" (without the quotes) to make sure that LEDIT can
handle the LIST command when no text has been entered.
3. Type "EXI". The program should NOT exit. If it exits, it's
probably because the "T" was left in the input buffer from the LIST
command. Your program is checking the first four characters of the
input buffer without checking the length of the typed command.
4. Type "ABC". The program should display a message about an illegal
or unrecognized command, or syntax error.
5. Type "0X" (zero followed by X). The program should reject this
line, displaying a message about an invalid line number. Type "0 X".
The same thing should happen. Try "30000X", then "30000 X", and then
"3000000000 X". The program should reject these, as well.
6. Type "-1 X" and "-1X". The program should reject these lines,
displaying a message either about an unrecognized command (or syntax
error), or an invalid line number.
7. Type a simple carriage return. There should be no effect, except
for any prompt being repeated.
8. Type the following exactly as shown. Note that lines 1000 and 100
each contain four spaces:
30 X
1 The
29999 ZZZZZZZZ
1000 used
100 is
10000 to test
30file --
1 This
29999 LEDIT.
9. Type "list". You should see the following, lined up exactly as
shown. There should be exactly five spaces for the line numbers.
1 This
30 file --
100 is
1000 used
10000 to test
29999 LEDIT.
-- continued --
Page 27
10. Try a line of text beginning with a number: "20000 123 45". Then
type " LIST" with three leading spaces. You should see
1 This
30 file --
100 is
1000 used
10000 to test
20000 123 45
29999 LEDIT.
11. Insert an empty line by typing "15000 ". Then type "LIST". You
should see
1 This
30 file --
100 is
1000 used
10000 to test
15000
20000 123 45
29999 LEDIT.
12. Type "EXIT ABC". LEDIT should display a error message and NOT
exit. The requirements say that the EXIT command must stand alone.
13. Type " exit". LEDIT should exit, and you should have a new
file with the output file name you gave in step 1. Type the file with
the TYPE command. You should see exactly this, starting in column 1:
This
file --
is
used
to test
123 45
LEDIT.
14. Run LEDIT again. Use for an input file the name of the OUTPUT
file you used in step 1. LEDIT should say "File found." Choose yet
another name for the output file and type "list". You should see:
10 This
20 file --
30 is
40 used
50 to test
60
70 123 45
80 LEDIT.
-- continued --
Page 28
15. Type " LIST 30". You should see only line 30. Type
"list25-45". You should see only lines 30 and 40. Type "LIST - 40".
You should see lines 10 through 40. Type "list35 -". You should see
lines 40 through 80. Type "list 15". The program should either do
nothing or display a message that line 15 doesn't exist. Try
"list 30000" and "list 3000000000". Again, the program should either
do nothing or display an error message. Now type "list" and check
that the result is the same as in step 14.
16. LEDIT should display an error message for each of the following:
LIST -
list 2A
list 30 - 50 -
list 30 - -50
list 30-A
LIST XYZ
17. Type "70" to delete line 70. Type "LIST". You should see
10 This
20 file --
30 is
40 used
50 to test
60
80 LEDIT.
18. Delete line 60. Do not list. Now delete line 80 and list. You
should see
10 This
20 file --
30 is
40 used
50 to test
19. Delete line 10 and list. You should see lines 20 through 50
above. Delete line 50 and list. You should see lines 20 through 40.
Delete line 30 and list. You should see lines 20 and 40. Delete line
40 and list. You should see only line 20. Delete line 20 and list.
The result should be the same as in step 2. Type "EXIT".
20. If your program passed all these tests, sincere congratulations!
You've completed a difficult assignment. We hope that you feel
comfortable with Ada now. If you like, you can compare your solution
with ours, starting on page 29 of these notes. When you go back to
ADA-TUTR, you'll learn to write your own generic packages, procedures,
and functions.
Page 29
LEDIT.ANS
---------
-- Our solution to Outside Assignment 5:
with Text_IO; use Text_IO;
procedure Ledit is
Max_Length : constant := 80;
Max_Line_Number : constant := 29_999;
type Text is
record
Len : Integer range 0 .. Max_Length := 0;
Val : String(1 .. Max_Length);
end record;
type Link;
type P is access Link;
type Link is
record
Num : Positive;
Line : Text;
Next : P;
end record;
Head : P := new Link;
Temp : P;
Input_File : File_Type;
Output_File : File_Type;
Input : Text;
Finished : Boolean := False;
Line_Num : Natural := 10;
function Str(T : in Text) return String is separate;
procedure Read_Input_File is separate;
procedure Do_Command is separate;
begin
Put("Input file: "); Get_Line(Input.Val, Input.Len);
Read_Input_File;
Put("Output file: "); Get_Line(Input.Val, Input.Len);
Create(Output_File, Name => Str(Input));
-- Get and process commands.
while not Finished loop
Put("> "); Get_Line(Input.Val, Input.Len);
Do_Command;
end loop;
-- Write the output file.
Temp := Head.Next; -- Skip unused link at start of linked list.
while Temp /= null loop
Put_Line(Output_File, Str(Temp.Line)); -- Write line of text.
Temp := Temp.Next; -- Get next link.
end loop;
Close(Output_File);
end Ledit;
-- continued --
Page 30
separate (Ledit)
function Str(T : in Text) return String is
begin
return T.Val(1 .. T.Len);
end Str;
separate (Ledit)
procedure Read_Input_File is
begin -- If the input file exists, display a message and read it in.
Open(Input_File, In_File, Str(Input));
Put_Line("File found.");
Temp := Head;
while not End_Of_File(Input_File) loop
Get_Line(Input_File, Input.Val, Input.Len); -- Read a line.
Temp.Next := new Link'(Line_Num, Input, null); -- Add to list.
Temp := Temp.Next; -- Advance pointer to next link.
Line_Num := Line_Num + 10;
end loop;
Close(Input_File);
exception -- If the input file doesn't exist, just display a message.
when Name_Error => Put_Line("File not found.");
end Read_Input_File;
separate (Ledit)
procedure Do_Command is
procedure Delete_First_Character(T : in out Text) is separate;
procedure Get_Leading_Integer(N : out Natural) is separate;
procedure Strip_Leading_Spaces_From_Input is separate;
procedure Add_Delete_Replace_Line is separate;
procedure List is separate;
begin
Strip_Leading_Spaces_From_Input;
if Str(Input) = "exit" or Str(Input) = "EXIT" then
Finished := True;
elsif Input.Len >= 4 and (Input.Val(1 .. 4) = "list" or
Input.Val(1 .. 4) = "LIST") then
List;
elsif Input.Len > 0 and Input.Val(1) not in '0' .. '9' then
Put_Line("Unrecognized command.");
elsif Input.Len > 0 then
Get_Leading_Integer(Line_Num);
if Line_Num not in 1 .. Max_Line_Number then
Put_Line("Illegal line number.");
else
Add_Delete_Replace_Line;
end if;
end if;
exception
when Numeric_Error | Constraint_Error =>
Put_Line("Line number too large.");
end Do_Command;
-- continued --
Page 31
separate (Ledit.Do_Command)
procedure Add_Delete_Replace_Line is
Inp : Text := Input;
begin
if Inp.Len > 0 and Inp.Val(1) = ' ' then -- Treat "9x" like "9 x".
Delete_First_Character(Inp);
end if;
Temp := Head; -- Find where this number belongs in linked list.
while Temp /= null and then Temp.Next /= null and then
Temp.Next.Num <= Line_Num loop
if Temp.Next.Num = Line_Num then
Temp.Next := Temp.Next.Next; -- Delete line.
else
Temp := Temp.Next; -- Advance to next link in list.
end if;
end loop;
if Input.Len > 0 then -- Add line.
Temp.Next := new Link'(Line_Num, Inp, Temp.Next);
end if;
end Add_Delete_Replace_Line;
separate (Ledit.Do_Command)
procedure Delete_First_Character(T : in out Text) is
begin
T.Val(1 .. T.Len - 1) := T.Val(2 .. T.LEN);
T.Len := T.Len - 1;
end Delete_First_Character;
separate (Ledit.Do_Command)
procedure Get_Leading_Integer(N : out Natural) is
Ans: Integer := 0;
begin
while Input.Len > 0 and Input.Val(1) in '0' .. '9' loop
Ans := Ans*10 + Character'Pos(Input.Val(1)) -Character'Pos('0');
Delete_First_Character(Input);
end loop;
N := Ans;
end Get_Leading_Integer;
separate (Ledit.Do_Command)
procedure Strip_Leading_Spaces_From_Input is
begin
while Input.Len > 0 and Input.Val(1) = ' ' loop
Delete_First_Character(Input);
end loop;
end Strip_Leading_Spaces_From_Input;
-- continued --
Page 32
separate (Ledit.Do_Command)
procedure List is
package IIO is new Integer_IO(Integer); use IIO;
Start, Finish : Natural;
Valid : Boolean := True;
begin
Input.Len := Input.Len - 4; -- Delete the name of the command.
Input.Val(1 .. Input.Len) := Input.Val(5 .. Input.Len + 4);
Strip_Leading_Spaces_From_Input;
if Input.Len = 0 then -- For "LIST" alone, list all lines.
Start := 0;
Finish := Max_Line_Number + 1;
else
Get_Leading_Integer(Start); -- Get number after "LIST".
Strip_Leading_Spaces_From_Input;
if Input.Len = 0 then -- For "LIST n", list only line n.
Finish := Start;
elsif Input.Val(1) /= '-' then -- Else "-" must follow n.
Valid := False;
else
Delete_First_Character(Input); -- Delete the "-".
Strip_Leading_Spaces_From_Input;
Get_Leading_Integer(Finish); -- Get number after "-".
Strip_Leading_Spaces_From_Input;
if Finish = 0 and Start = 0 then -- "LIST -" isn't valid.
Valid := False;
elsif Finish = 0 then -- For "LIST n -", list n through end.
Finish := Max_Line_Number + 1;
end if;
Valid := Valid and Input.Len = 0; -- No trailing garbage.
end if;
end if;
if not Valid then
Put_Line("Illegal syntax for LIST.");
else
Temp := Head.Next; -- Skip unused link at start of linked list.
while Temp /= null and then Temp.Num <= Finish loop
if Temp.Num >= Start then
Put(Temp.Num, Width => 5); -- Display line #, width 5.
Put_Line(' ' & Str(Temp.Line)); -- Display text of line.
end if;
Temp := Temp.Next; -- Get next link.
end loop;
end if;
exception
when Numeric_Error | Constraint_Error =>
Put_Line("Line number too large in List.");
end List;
Page 33
TASKING.DUM
-----------
with text_io, calendar; use text_io, calendar;
procedure tasking is
interval : constant duration := 5.0;
total_intervals : constant positive := 9;
start_time : constant time := clock;
quitting_time : constant time := start_time +
total_intervals*interval;
next_time : time := start_time;
task type tick is
entry make_noise;
entry shutdown;
end tick;
t : tick;
task body tick is
quit : boolean := false;
begin
while not quit loop
select
accept make_noise do
put_line("Tick!");
end make_noise;
or
accept shutdown;
quit := true;
end select;
end loop;
end tick;
begin
while next_time < quitting_time loop
t.make_noise;
next_time := next_time + interval;
put_line("(5-second delay)"); delay next_time - clock;
end loop;
t.shutdown;
end tasking;
Page 34
STEPS FOR OUTSIDE ASSIGNMENT 6, EXERCISE IN TASKING
1. Make a copy of TASKING.DUM by typing COPY TASKING.DUM TASKING.ADA.
Compile, link, and execute the program to make sure it displays
"Tick!" nine times, with a 5-second delay after each "Tick."
2. Edit TASKING.ADA to become your solution. Make your changes in
upper case.
3. Compile TASKING.ADA, link, and execute.
4. Compare your output with page 35 of these notes. If there are any
errors, go back to step 2.
5. When your output agrees with these notes, you've finished the
assignment and will have a chance to compare your solution with
ours.
Page 35
OUTPUT FROM TASKING.EXE AFTER MODIFICATION OF TASKING.ADA
C>tasking
Task number 1 is starting.
Task number 2 is starting.
Task number 3 is starting.
(5-second delay)
(5-second delay)
Task number 1 is starting.
(5-second delay)
Task number 2 is starting.
(5-second delay)
Task number 1 is starting.
Task number 3 is starting.
(5-second delay)
(5-second delay)
Task number 1 is starting.
Task number 2 is starting.
(5-second delay)
(5-second delay)
Task number 1 is starting.
Task number 3 is starting.
(5-second delay)
C>
Page 36
TASKING.ANS
-----------
-- Our solution to Outside Assignment 6:
with text_io, calendar; use text_io, calendar;
procedure tasking is
interval : constant duration := 5.0;
total_intervals : constant positive := 9;
start_time : constant time := clock;
quitting_time : constant time := start_time +
total_intervals*interval;
next_time : time := start_time;
task type tick is
ENTRY IDENTIFY(TASK_NUMBER : IN NATURAL);
entry shutdown;
end tick;
T : ARRAY(1 .. 3) OF TICK;
PERIOD : CONSTANT ARRAY(T'RANGE) OF POSITIVE := (2, 3, 4);
TIMER : ARRAY(T'RANGE) OF NATURAL := (OTHERS => 0);
task body tick is
quit : boolean := false;
begin
while not quit loop
select
ACCEPT IDENTIFY(TASK_NUMBER : IN NATURAL) DO
PUT_LINE("Task number" & INTEGER'IMAGE(TASK_NUMBER) &
" is starting.");
END IDENTIFY;
or
accept shutdown;
quit := true;
end select;
end loop;
end tick;
begin
while next_time < quitting_time loop
FOR I IN T'RANGE LOOP
IF TIMER(I) = 0 THEN
T(I).IDENTIFY(I);
TIMER(I) := PERIOD(I);
END IF;
TIMER(I) := TIMER(I) - 1;
END LOOP;
next_time := next_time + interval;
put_line("(5-second delay)"); delay next_time - clock;
end loop;
FOR I IN T'RANGE LOOP
T(I).SHUTDOWN;
END LOOP;
end tasking;
Page 37
APPENDIX A: WOULD YOU LIKE A TEXTBOOK TO USE ALONG WITH ADA-TUTR?
ADA-TUTR doesn't require a textbook, but a number of users have
asked us to recommend a book that would go well with this course. We
made a special arrangement with John Wiley and Sons so that you can
buy the excellent textbook RENDEZVOUS WITH ADA 95 by David Naiditch
for only $44.95 plus sales tax. Simply print the file COUPON.TXT, or
copy the coupon below. Please send it to the address on the coupon,
not to Software Innovations Technology.
+--------------------------------------------------------------------+
| |
| If you would like to have a textbook to go along with ADA-TUTR, |
| send this coupon to |
| |
| Attn.: Jane Andolsek, 4th floor |
| Wiley - Training Sales |
| John Wiley and Sons |
| 605 Third Avenue |
| New York, NY 10158 |
| |
| Please send me ______ copy(ies) of RENDEZVOUS WITH ADA 95, Second |
| Edition, by David Naiditch, to examine for 15 days. |
| |
| Order #: 1-01276-9. Price: $44.95 plus local sales tax, postage |
| and handling. |
| |
| [ ] Bill me. |
| |
| [ ] Payment enclosed. (Orders accompanied by payment will be |
| sent postpaid.) |
| |
| [ ] Charge my ___ VISA ___ MasterCard ___ American Express |
| (Note: Credit card orders are not considered prepayment.) |
| |
| Account #: ________________________________ |
| |
| Expiration Date: __________________________ |
| |
| NAME:____________________________ AFFILIATION:____________________ |
| |
| ADDRESS:__________________________________________________________ |
| |
| CITY/STATE/ZIP:_____________________ SIGNATURE:___________________ |
| (Offer invalid without signature) |
| |
| (Prices subject to change without notice and higher outside the |
| U.S.A.) |
| |
+--------------------------------------------------------------------+
Page 38
APPENDIX B: RUNNING BOOK TO PRINT ALL THE SCREENS
If you're running ADA-TUTR on a PC, you can always print the
screen that's currently being displayed by holding a Shift key while
pressing the PrtSc key.
However, several users wanted to be able to print ALL the screens
in the entire course, as one large book. So, starting with version
2.0 of ADA-TUTR, we added the program BOOK. This program reads the
file ADA_TUTR.DAT and produces two large output files, which can then
be printed. Be prepared to print about 500 pages!
Simply type BOOK to run the program.
BOOK creates plain ASCII files, double spaced the way this
-------------
paragraph is double spaced. It emphasizes text by placing hyphens
------------- ---------- -------
below the lines like this.
---------
BOOK asks you for the name of the output file for the first half
of the tutorial. After that file is written, BOOK asks for the name
of the output file for the second half of the tutorial. You can then
print the output files.
You can optionally specify the name PRN for both output files, to
have the output of BOOK go directly to the printer.
Begin reading your printed output with screen 104. Note that, at
the bottom of each page, there's a list of possible user responses
with screen numbers. For example, at the bottom of Screen 104 you'll
see
'Y' 107 'N' 122
This means that, if the user presses Y, screen 107 will be shown next,
and if the user presses N, screen 122 will be next. Pressing X always
takes the user to screen 103. The special "screen numbers" 098
through 100 appearing at the bottom of screens 107 and 108 are
explained in the introductory comments in ADA_TUTR.ADA.
BOOK, like ADA-TUTR, will run on a PC or on any Ada-speaking
computer.
Page 39
APPENDIX C: SOME ADA COMPILERS AVAILABLE FOR THE PC
Here's a brief list of DOS and Windows Ada compilers available
for the IBM PC and compatibles. For this course, an Ada compiler is
helpful, but not required. There are six Outside Assignments; most of
them ask you to write and compile a short Ada program. If you don't
have access to an Ada compiler, just skip the Outside Assignments.
Although we may now have a small financial interest in some of
the companies mentioned here (see page 5), we try to keep this list
unbiased. If you know of an Ada compiler for the PC that should be on
this list, or if you have any updated information or corrections,
please contact us.
1. Augusta is a free compiler/interpreter found on some computer
bulletin boards. Augusta implements a very small subset of Ada 83
with no packages, and some of the syntax is non-standard (e.g.,
"elseif" instead of "elsif"). Augusta isn't really adequate for
most of the Outside Assignments of ADA-TUTR.
2. Small Ada is another free compiler/interpreter implementing a
small subset of Ada 83. It is available at
http://wuarchive.wustl.edu/languages/ada/compiler/smallada. While
Small Ada is a larger subset that Augusta, we recommend more
complete compilers for those learning Ada.
3. New York University Ada/Ed is a free compiler/interpreter
implementing Ada 83 completely. GW-Ada/Ed is Ada/Ed with some
enhancements (such as an editor) added by George Washington
University, and is also free. Both Ada/Ed and GW-Ada/Ed are
available at
http://wuarchive.wustl.edu/languages/ada/compiler/adaed.
4. AVLAda9X is a free Ada 95 programming environment available at
http://wuarchive.wustl.edu/languages/ada/compiler/avlada9x.
5. GNAT is a popular free Ada 95 compiler that, in the DOS version,
is complete except for tasking. It is available at
http://wuarchive.wustl.edu/languages/ada/compiler/gnat, and by
anonymous FTP at cs.nyu.edu. Versions are also available for
Windows 95 and Windows NT at cs.nyu.edu and at
ftp.cs.yale.edu/pub/gnat. Additional tools for GNAT are sold by
LabTek Corp., 565 Wagon Trail, Orange, CT 06477-2171, starting at
$49.95. Phone 203-877-2880, fax 203-877-4625, e-mail
LabTek@cs.yale.edu.
6. Ez2load is a package containing GW-Ada/Ed, AVLAda9X, GNAT, and a
number of other Ada tools, available at
http://wuarchive.wustl.edu/languages/ada/compiler/ez2load, and by
FTP at ftp.gwu.edu in the directory pub/ada/ez2load.
-- continued --
Page 40
7. Open Ada is sold by D.C. Heath Co., 2700 N. Richardt Ave.,
Indianapolis, IN 46219-1186 for $66.67 plus tax and shipping.
Phone 800-334-3284, 800-428-8071, 317-359-5585, or 617-862-6650,
fax 800-824-7390.
8. ActivAda for Windows is sold by Thomson Software Products, 10251
Vista Sorrento Parkway, Suite 300, San Diego, CA 92121-2718,
starting at $99 ($75 for students) for the Personal Edition. This
edition does not have tasking and is intended for educational
purposes. A Professional Edition is also available. Phone
800-833-0042 or 617-221-7354. E-mail activada@thomsoft.com, Web
site http://www.thomsoft.com.
9. Janus/Ada is sold by R.R. Software, Inc., PO Box 1512, Madison, WI
53701-1512, starting at $129. Educational discounts are
available. Phone 800-722-3248 or 608-245-0375, e-mail
info@rrsoftware.com.
Page 41
APPENDIX D: SAMPLE MULTI-USER SOFTWARE LICENSE
Software Innovations Technology believes in a plain-English
software license that people can understand. A sample license appears
on the next page. However, if your company's lawyers would prefer a
differently worded license, simply contact us. We'll be happy to
accommodate you.
As you can see, the terms of our license are very generous. We
allow you to make as many copies of ADA-TUTR as you like, and to
install it on as many computers as you like, regardless of the
computer type (laptop, desktop, workstation, mainframe, etc.) Also,
you may use ADA-TUTR at all your geographical locations, which is why
we use the term Multi-User License rather than Site License. The only
thing we limit is the total number of people who may use ADA-TUTR
(except in the case of an unlimited license). Please remember that if
100 people use ADA-TUTR now and 100 others use it later, that counts
as 200 users, not 100.
In the sample license on the next page, comments appear in
brackets. These comments are not part of the actual license.
Page 42
[SAMPLE] MULTI-USER SOFTWARE LICENSE
Software Innovations Technology
1083 Mandarin Drive NE
Palm Bay, FL 32905-4706
(407) 951-0233
This license conveys to XYZ Company the right to use the comput-
er program ADA-TUTR, the Interactive Ada Tutor, under the fol-
lowing conditions:
1. The program shall remain the property of Software Innova-
tions Technology.
2. The program may be freely copied and installed on any or all
computers of XYZ Company, regardless of geographical location.
Copies of the program may be distributed outside XYZ Company for
others to evaluate on a trial basis.
3. This license is perpetual.
4. The number of XYZ Company personnel [or "students and per-
sonnel of XYZ University"] who may use the program is unlimited.
[*** OR ***]
4. The total number of XYZ Company personnel [or "students and
personnel of XYZ University"] who use the program, excluding
those who only briefly evaluate it, shall not exceed two thou-
sand (2000) [or "five hundred (500)" or "one hundred (100)"].
In calculating the number of users, XYZ Company must always add
the number of people currently using the program to the number
who have used it in the past.
5. [This paragraph is omitted in unlimited licenses.] Software
Innovations Technology sells licenses for larger numbers of
users. XYZ Company may, at any time, upgrade this license for a
larger number of users by paying, to Software Innovations Tech-
nology, the difference in price between the two licenses.
6. This license is not transferable.
7. The Disclaimer of Warranty, included in the program documen-
tation, applies as if included in this license.
8. [This paragraph is omitted if the order is prepaid.] This
license shall be void if the invoice is not paid within thirty
(30) days of the date of this license.
(Signed)________________________________ Date _________________
John J. Herro, President
Software Innovations Technology
Page 43
APPENDIX E: INSTALLING ADA-TUTR ON OTHER COMPUTERS
Because source code is included with ADA-TUTR, you can install
ADA-TUTR on almost any computer that has an Ada 83 or Ada 95 compiler,
from compact units to large mainframes. ADA-TUTR is written in Ada
(of course!), and Ada programs tend to be very portable.
The screen should have at least 24 rows of 80 characters each,
and should support the ANSI escape sequences for highlighting, reverse
video, cursor positioning, etc. Almost all computers qualify.
You need a way to send text files from your PC to the other
computer. Files are usually sent to large mainframes with the program
KERMIT, which is in the public domain. One version of KERMIT runs on
the PC, another on the mainframe. The two communicate with each
other. Sometimes XMODEM or another protocol is used instead of
KERMIT.
If a communications program is available on the PC but not on the
other computer, you can still send text files, but not as efficiently.
Connect your PC as a terminal and tell the other computer to copy the
keyboard to a new file. For example, on a VAX computer running VMS,
the command is "COPY TT: filename.ext" (without the quotes). Then
tell your communications program to send (or "upload") the file, using
ASCII protocol, and stripping line feeds. After the file is sent,
type the appropriate end-of-file character for the other computer. On
a VAX running VMS, you would type Control-Z. Since this method of
file transfer doesn't do error checking, you may have to send each
file twice using two different names, and compare the two files with
each other on the other computer. It's far better to use a
communications program on the other computer, if one is available.
OK, you have some means of sending text files. You won't have to
send other kinds of files. To install ADA-TUTR on the other computer,
just follow these steps:
1. Type DAT2TXT on the PC. This will read the file ADA_TUTR.DAT,
which is not a text file, and create a new text file TUTOR.TXT, which
can be sent to the other computer. DAT2TXT will probably take several
minutes to run. Don't try to bypass this step even if you're able to
send non-text files. It's important that you send TUTOR.TXT and not
ADA_TUTR.DAT, because the PC version of ADA_TUTR.DAT doesn't work on
some systems.
2. Decide where ADA-TUTR will reside on the other computer, so that
it will be available to all users. For example, on a VAX computer
running VMS, the tutor is usually placed in SYS$INSTRUCTION. It could
also be placed in SYS$MANAGER, etc.
-- continued --
Page 44
3. Send to the other computer all the files that come with ADA-TUTR,
except ADA_TUTR.DAT and the files ending in .COM. Be sure to send
TUTOR.TXT, created in step 1. Don't try to send ADA_TUTR.DAT and the
.COM files, because they're not text files. Also note that ADA-TUTR
creates a file ADA_TUTR.USR the first time it's run. Do NOT send that
file, because it also is not a text file. If our file names are
illegal on the other system, you'll have to rename them as they're
sent. (We haven't seen a system where our file names are illegal.)
4. If any of the file names ADA_TUTR.DAT, ADA_TUTR.USR, or TUTOR.TXT
are illegal on your system, edit the files ADA_TUTR.ADA, BOOK.ADA,
CHANGESN.ADA, and TXT2DAT.ADA to show new, legal file names. Search
for the old file names and replace them with the new ones.
5. Note that each user will probably run ADA-TUTR from his or her own
file directory. There will be a file ADA_TUTR.USR in each user's
directory, but all the other files will reside in a common area, such
as SYS$INSTRUCTION, etc. Therefore, edit the files ADA_TUTR.ADA,
BOOK.ADA, CHANGESN.ADA, and TXT2DAT.ADA to show the full path name of
the file ADA_TUTR.DAT. On a VAX system, for example, you might search
for the name ADA_TUTR.DAT and replace it with
SYS$INSTRUCTION:ADA_TUTR.DAT.
6. On one system we encountered, Ada source file names must end in .a
rather than .ADA. If that's the case on your system, rename all the
.ADA files.
7. On the other computer, compile, link, and run TXT2DAT.ADA. Some
systems call the linking step "binding." This program will read
TUTOR.TXT and create ADA_TUTR.DAT on the other system. If you wish,
you may then delete TUTOR.TXT.
8. If you're using VAX Ada, skip to page 46. If you're using a UNIX
based system that has a C compiler with the ability to link into Ada
programs, skip to page 47. Otherwise, continue with the next page.
If you're not certain, continue with the next page.
-- continued --
Page 45
9. Compile VANILLA.ADA, but don't try to link or run it. This "plain
vanilla" file will work with any standard Ada compiler.
10. Compile ADA_TUTR.ADA.
11. Link, giving the name of the main program, ADA_TUTR. This will
produce an executable file, which on most systems will be called
ADA_TUTR.EXE. However, on a few systems the executable file will have
a strange name, so rename the executable file if necessary.
12. Run ADA-TUTR. The command is usually ADA_TUTR or RUN ADA_TUTR.
Note that, on some systems, you'll have to strike ENTER (or RETURN or
NEW-LINE) after each response, even though the program says, "You need
not hit ENTER." The reason is that some systems require you to strike
ENTER when they read the keyboard through TEXT_IO. Also note that some
of the tutorial text refers to commands that are meant for a PC, such
as COPY and PRINT. These commands may have to be varied slightly to
work on your system.
13. Define a command so that each user will be able to run ADA-TUTR
easily.
We'll be happy to help you install ADA-TUTR and get it running, even
if you haven't registered or bought a license. We want you to try
ADA-TUTR before paying for it.
Page 46
SPECIAL INSTRUCTIONS FOR VAX ADA
9. If you're using VAX Ada, compile VAX.ADA, but don't try to link or
run it. The file VANILLA.ADA would work, but VAX.ADA makes it
unnecessary to strike RETURN after each response. The reason is that
VAX.ADA gets characters from the keyboard by calling the System
Service routines, without going through TEXT_IO.
10. Compile ADA_TUTR.ADA.
11. Link, giving the name of the main program, ADA_TUTR. The command
is ACS LINK ADA_TUTR. This will produce an executable file,
ADA_TUTR.EXE.
12. Run ADA-TUTR by typing RUN ADA_TUTR. Note that some of the
tutorial text refers to commands that are meant for a PC, such as COPY
and PRINT. These commands may have to be varied slightly to work on
your system. For example, when the text says to type
COPY TRITYPE.DUM TRITYPE.ADA, your users might have to type
COPY SYS$INSTRUCTION:TRITYPE.DUM TRITYPE.ADA.
13. Define a command so that each user will be able to run ADA-TUTR
easily. For example, you might add the following line to SYLOGIN.COM:
$ TEACHADA :== "RUN SYS$INSTRUCTION:ADA_TUTR"
We'll be happy to help you install ADA-TUTR and get it running, even
if you haven't registered or bought a license. We want you to try
ADA-TUTR before paying for it.
Page 47
SPECIAL INSTRUCTIONS FOR UNIX BASED SYSTEMS
9. If you're using a UNIX based system with a C compiler, and your
Ada compiler can link with modules produced by C, compile ONECHAR.C
with the C compiler. If you can't compile ONECHAR.C because your
system doesn't have the include file TERMIO.H, then compile ALTCHAR.C
instead. However, if you compile ALTCHAR.C, you'll have to invoke
ADA-TUTR between two calls to stty, in a script file. For example:
stty raw
/usr/local/ada-tutr/ada_tutr
stty -raw
10. Compile UNIX.ADA, but don't try to link or run it. The file
VANILLA.ADA would work, but UNIX.ADA, when used with ONECHAR.C or
ALTCHAR.C, makes it unnecessary to strike RETURN after each response.
The reason is that ONECHAR.C and ALTCHAR.C get characters directly
from the keyboard, without going through TEXT_IO.
11. Compile ADA_TUTR.ADA.
12. Link, giving the name of the main program, ADA_TUTR. Include the
object file from the C compiler in the link. This will create an
executable file, called ADA_TUTR on most systems. (On one system we
encountered, the file created was named a.out.) If necessary, rename
the executable file to an appropriate name, such as ada_tutr or
ADA_TUTR or ADA_TUTR.EXE.
Here are sample steps 9 through 12 for two UNIX based systems:
ON A UNISYS 5000/80: ON A HARRIS HCX:
cc -c onechar.c cc -c onechar.c
ada unix.ada ada_tutr.ada ada unix.a ada_tutr.a
ald -p'onechar' ada_tutr a.ld ada_tutr onechar.o
mv a.out ada_tutr
13. Run ADA-TUTR. Usually this is done by typing ada_tutr. Note
that some of the tutorial text refers to commands that are meant for a
PC, such as COPY and PRINT. These commands may have to be varied
slightly to work on your system.
14. Define a command so that each user will be able to run ADA-TUTR
easily.
We'll be happy to help you install ADA-TUTR and get it running, even
if you haven't registered or bought a license. We want you to try
ADA-TUTR before paying for it.
Page 48
APPENDIX F: WE LISTEN! NEW FEATURES OF ADA-TUTR
Almost all of the features added to ADA-TUTR were done as a
result of your comments and suggestions. Thank you very much, and
please continue to send them! We want to produce the very best Ada
Tutor possible. Here's a history of revisions to ADA-TUTR. In
addition to the changes shown here, we're constantly making minor
improvements in the tutorial text and updating the list of Ada
compilers available for the PC (page 39).
VERSION 3.11:
Added our e-mail addresses and recompiled with Janus/Ada.
VERSION 3.10:
Changed all Ada 9X references Ada 95.
Added comparisons of Ada with C and C++.
Changed the documentation to assume that all customers now want
3.5-inch diskettes, unless they check the 5.25-inch diskette box in
INVOICE.TXT.
VERSION 3.00:
Added much new material to cover Ada 9X, and changed the
capitalization style of all Ada code from that of the Ada 83 RM to
that of the Ada 9X RM.
Removed references to PC-Write, because recent versions of DOS
include a screen editor.
Replaced JANUS.ADA with JANUS16.PKG and JANUS32.PKG.
VERSION 2.02:
Added the files DESC.SDI and FILE_ID.DIZ, briefly describing
ADA-TUTR.
Improved ED.ADA to take advantage of the newest version PC-Write,
called PC-Write Advanced Level.
VERSION 2.01:
Added the file JANUS.ADA.
VERSION 2.00:
Changed the ADA_TUTR.DAT file to a more compact format, so that
ADA-TUTR would take less room on a hard disk and be able to run from a
floppy diskette.
Added the BOOK program to print all of the tutorial screens as a
large book (page 38).
Updated Outside Assignment 6 to make it easier to see where the
5-second intervals occur.
Added the sample Multi-User License (pages 41-42).
Added the special offer for a textbook (page 37).
VERSION 1.22:
Added ALTCHAR.C for UNIX based systems that don't have the file
TERMIO.H. (Thank you, Mr. Richard Conn, 22 Hunt Court, Tinton Falls,
NJ 07753.)
Page 49
VERSION 1.21:
Added ONECHAR.C so users of UNIX based systems wouldn't have to
strike ENTER after each response. (Thank you, Mr. David Hill, 7549
Wynford Street, Salt Lake City, UT 84121.)
Changed all hyphens in file names to underlines, at the request
of PC-SIG, so that ADA-TUTR would be compatible with High Sierra
Format CD-ROMs.
VERSION 1.20:
Added the ability to select background, foreground, and border
colors on a PC without sacrificing compatibility with mainframe
computers and monochrome PCs.
Made ADA-TUTR automatically remember where you leave off when you
exit the program, so that you don't have to write down the screen
number. Also made ADA-TUTR remember your color choices.
Made ADA-TUTR tell you the current screen number and the
approximate percentage through the course when you strike X, and added
the ability to go back to the last question or to the last Outside
Assignment.
Added a special screen at the start of each major section,
showing an outline of the course.
Updated the tutorial text to reflect the government's new policy
regarding expiration of Validation Certificates.
Updated the tutorial text to show that the newest Ada compilers
now raise CONSTRAINT_ERROR where NUMERIC_ERROR was once raised.
Added the offer for customized versions of ADA-TUTR, and for the
Customization Kit (page 4).
Recompiled ADA-TUTR using a validated compiler from Meridian
Software Systems, Inc., instead of an older unvalidated compiler from
Artek, Inc.
Made ADA-TUTR available on 3.5" as well as 5.25" diskettes.
VERSION 1.01:
Fixed minor typographical errors in Version 1.00.
Page 50
APPENDIX G: DISCLAIMER OF WARRANTY
We hate legal mumbo jumbo, but we have to say the following to protect
ourselves:
Software Innovations Technology makes no warranty of any kind,
expressed or implied, including any warranties of merchantability or
fitness for a particular purpose. We shall not be liable for any
damages, whether direct, indirect, incidental, special, or
consequential, arising from a failure of this program to operate in a
manner desired by the user. We shall not be liable for any damage to
data or property which may be caused directly or indirectly by use of
this program. IN NO EVENT SHALL SOFTWARE INNOVATIONS TECHNOLOGY BE
LIABLE FOR ANY DAMAGES, INCLUDING ANY LOST PROFITS, LOST SAVINGS, OR
OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
INABILITY TO USE THIS PROGRAM, OR FOR ANY CLAIM BY ANY OTHER PARTY.